$(function(){
	$('.login').click(function(){
		login();
	});
});
/**
 * 联系我们
 */
function connectMe(){
	layer.open({
    	extend:['skin/layer_iframe.css'],
        skin:'layer-ext-skin-layer_iframe.css',
        type:2,
        title:'联系我们',
        area:['800px','400px'],

        content:$("#path").val()+'/framework/connectUs.do',
        maxmin: true,
        shadeClose: true

    });
}
/**
 * 凭证管理
 */
function licenceManager(){
	layer.open({
    	extend:['skin/layer_iframe.css'],
        skin:'layer-ext-skin-layer_iframe.css',
        type:2,
        title:'添加凭证',
        area:['500px','400px'],

        content:$("#path").val()+'/framework/licenceManager.do',
        maxmin: true,
        shadeClose: true

    });
}
function login(){
	//凭证校验
	/*if(!checkLicence()){
		layer.msg("凭证失效，请上传新凭证！",{time:3000});
		return;
	}*/
	var path = $('.login').attr('path');
	$.ajax({
		url:path+"/framework/ajaxLoginKey.do",
		type:"post",
		async: false,
		dataType: "json",
		beforeSend:function(){
			//$('.login').attr('onclick','');
		},
		error:function(data){  
			layer.msg('出错了！！',{time:2000});
	    },
		success:function(data){
			var key = data.key;
			testLogin(key);
		},
	});
	
}
/**
 * 凭证校验，返回true校验成功，false校验失败
 */
function checkLicence(){
	var flag = false;
	$.ajax({
		url:$("#path").val()+"/cmdb/licence/checkLicence.do",
		type:"post",
		async: false,
		dataType: "json",
		beforeSend:function(){
		},
		error:function(data){  
			layer.msg('出错了！！',{time:2000});
	    },
		success:function(data){
			flag = data.flag;
		},
	});
	return flag;
}

document.onkeydown = function(e) {   
    // 兼容FF和IE和Opera    
    var theEvent = e || window.event;    
    var code = theEvent.keyCode || theEvent.which || theEvent.charCode;//获取键盘code值     
    if (code == 13) {  //如果是回车键 
    	if($('#lgName').val()==""){
    		$('#lgName').focus();
    	}else if($('#lgPwd').val()==""){
    		$('#lgPwd').focus();
    	}else{
    		login();
    		return false;  
    	}
    }    
    return true;    
}

/**
 * 用户登录校验
 * @param key
 * @returns {Boolean}
 */
function testLogin(key){
	
	var path = $('.login').attr('path');
	var lgName=$("#lgName").val();
	var lgPwd=$("#lgPwd").val();
	
	if(lgName == "" ){
		layer.msg("请输入用户名！");
		return false;
	}
	
	if(lgPwd==""){
		layer.msg("请输入密码！");
		return false;
	}
	
	var pd=b64_md5(lgPwd);
	var pwd=b64_md5(pd+"-"+key);
	$("#Pwd").attr("value",pwd);
	
	$.ajax({				
		url:path+"/framework/checkLoginInfo.do",
		type:"post",
		async: false,
		data : {
			"personCode" : lgName,
			"personPassword" : pwd
		},
		dataType: "json",
		beforeSend:function(){
			//$('.login').attr('onclick','');
		},
		error:function(data){  
			layer.msg("校验出错了！！");
	    },
		success:function(data){
			var error = data.flag;
			if(error == true){
				var action_url = path+"/framework/loginSuccess.do";
				var $form = $("#loginform");
				$form.attr("method", "post");
				$form.attr("action", action_url);
				$form.submit();
			}else{
				layer.msg(error, {"icon": 2});
			}
		},
	});
}